package com.wa.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Service;

/**
 *  Spring中使用两种Aware接口自定义获取bean
 */
@Service
public class BeanFactoryAwareTest implements BeanFactoryAware {

    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public static Object getBean(String beanName) {
        if (beanFactory == null) {
            throw new NullPointerException("BeanFactory is null!");
        }
        return beanFactory.getBean(beanName);
    }
}